//
//  WBStatusViewModel.swift
//  weibo
//
//  Created by wenquan wang on 12/22/16.
//  Copyright © 2016 wenquan wang. All rights reserved.
//

import Foundation


class WBStatusViewModel:CustomStringConvertible {
    var status: WBStatus
    var memberIcon: UIImage?
    //-1 not varify 0 varified, 2 3 5 corp, 220 : da ren
    var vipIcon: UIImage?
    
    var retweetedStr: String?
    var commentStr: String?
    var likeStr: String?
    
    var pictureViewSize = CGSize()
    
    var picURLs: [WBStatusPicture]? {
        return status.retweeted_status?.pic_urls ?? status.pic_urls
    }
    var retweetedText: String?
    
    var rowHeight: CGFloat = 0
    
    init(model: WBStatus) {
        self.status = model
        if (model.user?.mbrank ?? 0) > 0 && (model.user?.mbrank ?? 0) < 7 {
            let imageName = "common_icon_membership_level\(model.user?.mbrank ?? 1)"
            memberIcon = UIImage(named: imageName)
        }
        switch model.user?.verified_type ?? -1 {
        case 0:
            vipIcon = UIImage(named: "avatar_vip")
        case 2,3,5:
            vipIcon = UIImage(named: "avatar_enterprise_vip")
        case 220:
            vipIcon = UIImage(named: "avatar_grassroot")
        default:
            break
            
        }
        retweetedStr = countString(count: status.reposts_count, defaultStr: "Retweet")
        commentStr = countString(count: status.comments_count, defaultStr: "Comment")
        likeStr = countString(count: status.attitudes_count, defaultStr: "Like")
        
        pictureViewSize = calcPictureViewSize(count: picURLs?.count)
        
        retweetedText = "@" + (status.retweeted_status?.user?.screen_name ?? "" )
            + ":"
            + (status.retweeted_status?.text ?? "")
        
        updateRowHeight()
        
    }
    
    private func countString(count: Int, defaultStr: String) -> String{
        if( count == 0){
            return defaultStr
        }
        if count < 10000 {
            return count.description
        }
        return String(format: "%.02f wan", Double(count) / 10000)
    }
    
    private func calcPictureViewSize(count: Int?) -> CGSize{
        if count == nil || count == 0{
            return CGSize()
        }
        // 1 2 3 = 0 1 2 / 3 = 0 + 1 = 1
        // 4 5 6 = 3 4 5 / 3 = 1 + 1 = 2
        // 7 8 9 = 6 7 8 / 3 = 2 + 1 = 3
        let row = ((count! - 1) / 3 )  + 1
        
        //calc height by row
        let height = WBStatusPictureViewOutterMargin
            +  CGFloat(row) * WBStatusPictureItemWidth
            + CGFloat((row - 1)) * WBStatusPictureViewInnerMargin
        
        return CGSize(width: WBStatusPictureViewWidth, height: height)
    }
    func updateSingleImageSize(image: UIImage){
        var size = image.size
        
        let maxWidth: CGFloat = 300
        let minWidth: CGFloat = 40
        if size.width > maxWidth {
            size.height = size.width * image.size.height / image.size.width
        }
        
        if size.width < minWidth {
            size.width = minWidth
            
            size.height = size.width * image.size.height / image.size.width / 4
        }
        
        size.height += WBStatusPictureViewOutterMargin
        
        pictureViewSize = size
        
        updateRowHeight()
        
    }
    var description: String{
        return status.description
    }
    func updateRowHeight() {
        let margin: CGFloat = 12
        let iconHeight: CGFloat = 34
        let toolbarHeight: CGFloat = 35
        
        let viewSize = CGSize(width: UIScreen.cz_screenWidth() - 2*margin, height: CGFloat(MAXFLOAT))
        let originalFont = UIFont.systemFont(ofSize: 15)
        let retweetedFont = UIFont.systemFont(ofSize: 14)
        var height: CGFloat = 0
        //1 top
        height = 2 * margin + iconHeight + margin
        //2 text
        if let text = status.text {
            height += (text as NSString).boundingRect(with: viewSize,
                                            options: [.usesLineFragmentOrigin],
                                            attributes: [NSFontAttributeName: originalFont],
                                            context: nil).height
        }
        //3 check retweet
        if status.retweeted_status != nil {
            height += 2 * margin
            
            if let text = retweetedText {
                height += (text as NSString).boundingRect(with: viewSize,
                                                options: [.usesLineFragmentOrigin],
                                                attributes: [NSFontAttributeName: retweetedFont],
                                                context: nil).height
            }
        }
        //picView
        height += pictureViewSize.height
        
        height += margin
        
        //bottom bar height
        height += toolbarHeight
        
        rowHeight = height
    }
}
